#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <iostream>
#include <unistd.h>
#include <string.h>

using namespace std;

int main()
{
    const char * filename = "./test1Fifo";

    /* 判断管道文件是否存在 */
    int temp = access(filename, F_OK);
    if(temp != 0)   //文件不存在则创建
    {
        int ret = mkfifo(filename, 0644);
        if(ret == -1)
        {

            perror("mkfifo error:");
            exit(-1);
        }
    }

    int fd = open(filename, O_WRONLY);      //不用写0644，权限因为只在创建的时候有用
    if(fd == -1)
    {
        perror("open error:");
        _exit(-1);
    }
    char buf[20];
    int writeBytes = 0;
    while(1)
    {
        cin >> buf;
        writeBytes =  write(fd, buf, strlen(buf));
        cout << "writeBytes = " << writeBytes << endl;
        sleep(1);
    }

    return 0;
}